<template>
  <ul class="list">
    <li class="itme" v-for="itme of lessCity" :key="itme" :ref="itme" @click="handelClick" @touchstart="handelTouchStart" @touchmove="handelTouchMove" @touchend="handelTouchEnd">{{itme}}</li>
  </ul>
</template>

<script>
export default {
  name: 'CityRight',
  props: ['cities'],
  data () {
    return {
      touchStaus: false,
      offsetTops: 0,
      timer: null
    }
  },
  updated () {
    this.offsetTops = this.$refs['A'][0].offsetTop
  },
  methods: {
    handelClick (e) {
      this.$emit('change', e.target.innerHTML)
    },
    handelTouchStart () {
      this.touchStaus = true
    },
    handelTouchMove (e) {
      if (this.touchStaus) {
        /* 函数节流 */
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 79
          const index = Math.floor((touchY - this.offsetTops) / 20)
          if (index >= 0 && index < this.lessCity.length) {
            this.$emit('change', this.lessCity[index])
          }
        }, 16)
      }
    },
    handelTouchEnd () {
      this.touchStaus = false
    }
  },
  computed: {
    lessCity () {
      const less = []
      for (let i in this.cities) {
        less.push(i)
      }
      return less
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/config.styl'
  .list
    position: absolute
    right: 0
    top: 1.58rem
    bottom: 0
    display:block
    display: flex
    flex-direction: column
    justify-content: center
    text-align: center
    width: .4rem
    .itme
      height: .4rem
      width: .4rem
      color: $background
</style>
